package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMNgayTrongTuanService;
import com.lb.jwitter.service.TblCTDTDMPhongService;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinTKB;
import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.service.TblThongTinTKBService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblThongTinNhomLopMonHoctblThongTinTKBChiTietInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}
	private int id;
	private int tKBId;
	private int phongHocId;
	private int ngayHocId;
	private int tietBatDauId;
	private int soTietHoc;
	private String maCoSo;
	private String tuanHoc;
	

	public String getTuanHoc() {
		return tuanHoc;
	}

	public void setTuanHoc(String tuanHoc) {
		this.tuanHoc = tuanHoc;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int gettKBId() {
		return tKBId;
	}

	public void settKBId(int tKBId) {
		this.tKBId = tKBId;
	}

	public int getPhongHocId() {
		return phongHocId;
	}

	public void setPhongHocId(int phongHocId) {
		this.phongHocId = phongHocId;
	}

	public int getNgayHocId() {
		return ngayHocId;
	}

	public void setNgayHocId(int ngayHocId) {
		this.ngayHocId = ngayHocId;
	}

	public int getTietBatDauId() {
		return tietBatDauId;
	}

	public void setTietBatDauId(int tietBatDauId) {
		this.tietBatDauId = tietBatDauId;
	}

	public int getSoTietHoc() {
		return soTietHoc;
	}

	public void setSoTietHoc(int soTietHoc) {
		this.soTietHoc = soTietHoc;
	}

	public String getMaCoSo() {
		return maCoSo;
	}

	public void setMaCoSo(String maCoSo) {
		this.maCoSo = maCoSo;
	}
	
private List<TblThongTinTKBChiTiet> tblThongTinTKBChiTiets;
	
	public List<TblThongTinTKBChiTiet> getTblThongTinTKBChiTiets() {
		return tblThongTinTKBChiTiets;
	}

	private TblThongTinTKBService tblThongTinTKBService;
	private TblThongTinTKBChiTietService tblThongTinTKBChiTietService;
	private TblCTDTDMTietHocService tblCTDTDMTietHocService;
	private TblCTDTDMNgayTrongTuanService tblCTDTDMNgayTrongTuanService;
	private TblCTDTDMPhongService tblCTDTDMPhongService;

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_ThongTin_TKB_ChiTiet",
					"them")) {
				return "noprivilege";
			}
			
			tblThongTinTKBService = (TblThongTinTKBService) context.getBean("tbl_ThongTin_TKBClient"); 


	        tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService) context.getBean("tbl_ThongTin_TKB_ChiTietClient"); 

	        tblCTDTDMPhongService = (TblCTDTDMPhongService) context.getBean("tbl_CTDT_DM_PhongClient"); 

	        tblCTDTDMNgayTrongTuanService = (TblCTDTDMNgayTrongTuanService) context.getBean("tbl_CTDT_DM_NgayTrongTuanClient"); 

	        tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient"); 
	        
	        TblThongTinTKBChiTiet tblThongTinTKBChiTiet = new TblThongTinTKBChiTiet();
	        tblThongTinTKBChiTiet.setMaCoSo(maCoSo);
	        tblThongTinTKBChiTiet.setNgayHocId(tblCTDTDMNgayTrongTuanService.gettblCTDTDMNgayTrongTuan(new Long(ngayHocId)));
	        tblThongTinTKBChiTiet.setPhongHocId(tblCTDTDMPhongService.gettblCTDTDMPhong(new Long(phongHocId)));
	        tblThongTinTKBChiTiet.setSoTietHoc(soTietHoc);
	        tblThongTinTKBChiTiet.setTietBatDauId(tblCTDTDMTietHocService.gettblCTDTDMTietHoc(new Long(tietBatDauId)));
	        tblThongTinTKBChiTiet.setTKBId(tblThongTinTKBService.gettblThongTinTKB(new Long(tKBId)));
	        tblThongTinTKBChiTiet.setTuanHoc(Integer.parseInt(tuanHoc, 2));
	        
	        Long id = tblThongTinTKBChiTietService.createtblThongTinTKBChiTiet(tblThongTinTKBChiTiet);
	        
	        tblThongTinTKBChiTiet.setId(id);
	        
	        if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
	        cons.clear(); 
	           if (id != 0) 
	           { 
	               ConditionReport con = new ConditionReport(); 
	               con.setCol("TKBId.id"); 
	               con.setValue1(tblThongTinTKBChiTiet.getTKBId().getId()); 
	               con.setOperator("eq"); 
	               cons.add(con); 
	           } 
	           { 
	               ConditionReport con = new ConditionReport(); 
	               con.setCol("ngayHocId.STT"); 
	               con.setValue1("asc"); 
	               con.setOperator("order"); 
	               cons.add(con); 
	           } 
	           { 
	               ConditionReport con = new ConditionReport(); 
	               con.setCol("tietBatDauId.STT"); 
	               con.setValue1("asc"); 
	               con.setOperator("order"); 
	               cons.add(con); 
	           } 
	           tblThongTinTKBChiTiets = tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons,pageSize,page);
	           pages = tblThongTinTKBChiTietService.counttblThongTinTKBChiTietByCondition(cons)/ pageSize + 1;

	           dataString = "";
				dataString += "&id=";
				dataString += "&tKBId=";
				dataString += "&phongHocId=";
				dataString += "&ngayHocId=";
				dataString += "&tietBatDauId=";
				dataString += "&soTietHoc=";
				dataString += "&maCoSo=";
	        return Action.SUCCESS; 
	        
		} catch (Exception e) {
			System.out
					.println("Loi Insert tbl_ThongTin_TKB_ChiTiet Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
	
	public String tuanHocString(Integer tuanHoc)
 	{
 		String res = "";
 		if(tuanHoc == null){
 			tuanHoc = 0;
 		}
 		
 		List<String> tuanHocs = new ArrayList<String>();
	    String s = Integer.toBinaryString(tuanHoc);
	    tuanHocs = new ArrayList<String>(Arrays.asList(s.split("(?!^)")));
	    int delta = 26 - tuanHocs.size();
        for(int i = 0; i < tuanHocs.size(); i++){
        	res += tuanHocs.get(i).equalsIgnoreCase("1") ? ((i+1+delta)%10) : "-";
        }
        if(tuanHocs.size() < 26){
        	for(int j = tuanHocs.size(); j < 26; j++){
        		res = "-" + res;
        	}
        } 
        return res;
 	}
 	
 	public TblThongTinNhomLopMonHoctblThongTinTKBChiTietInsertAction getThis(){
 		return this;
 	}
}
